package com.visizen.wisdomclass.server.action.User;

import com.alibaba.fastjson.JSONObject;
import com.visizen.wisdomclass.server.action.response.ErrorCode;
import com.visizen.wisdomclass.server.action.response.ErrorCodeManager;
import com.visizen.wisdomclass.server.action.response.Result;
import com.visizen.wisdomclass.server.annotation.ValidateToken;
import com.visizen.wisdomclass.server.model.WcUser;
import com.visizen.wisdomclass.server.service.UserService;
import com.visizen.wisdomclass.server.util.WCDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 【用户】 获取指定用户的详细信息
 *
 * @author: chenjian
 */
@Controller
@RequestMapping("/User")
public class UserGetUserInfoAction {
    /**
     * 根据用户ID获取其详细信息
     *
     * @param token
     * @param userId
     * @param request
     * @return
     */
    @ValidateToken
    @RequestMapping(value = "/getUserInfoById", method = RequestMethod.GET)
    @ResponseBody
    public Result getUserInfoById(@RequestParam(required = true) String token,
                                  @RequestParam(required = true) long userId,
                                  HttpServletRequest request) {
        Result result = new Result();
        JSONObject jo = new JSONObject();
        WcUser user = userService.getUserById(userId);
        if (null == user) {
            /**
             * 获取指定用户的信息失败
             */
            jo.put("exception", "");
            ErrorCodeManager codeManager = ErrorCodeManager.getLocalMsg(ErrorCode.C1031_getUserInfoByIdFailed);
            result.setFailedResult(request, codeManager.getCode(), codeManager.getMsg(), jo);
        } else {
            /**
             * 获取用户的信息成功
             */
            jo.put("userId", user.getUser_id());
            jo.put("fullname", user.getFullname());
            jo.put("attachId", user.getAttach_id());
            jo.put("sex", user.getSex());
            jo.put("birthday", WCDate.toDateString(user.getBirthday()));
            jo.put("mobilephone", user.getMobilephone());
            jo.put("email", user.getEmail());

            result.setSuccessResult(request, "", jo);
        }
        return result;
    }

    @Autowired
    private UserService userService;
}
